package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.zbkj.dal.base.enums.ParameterInputTypeEnum;
import com.zbkj.dal.entity.product.Parameter;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.ParameterQueryRequest;
import com.zbkj.dal.pojo.response.ba.ParameterResponse;
import com.zbkj.dal.common.utils.PageInfoUtil;
import com.zbkj.dal.dao.ParameterMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.service.service.ParameterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 参数 服务实现类
 * </p>
 *
 * @author jhui
 * @since 2023-10-31
 */
@Service
public class ParameterServiceImpl extends ServiceImpl<ParameterMapper, Parameter> implements ParameterService {

    @Override
    public PageInfo<ParameterResponse> pageParameters(ParameterQueryRequest parameterQueryRequest, PageParamRequest pageParamRequest) {
        LambdaQueryWrapper<Parameter> queryWrapper = Wrappers.lambdaQuery(Parameter.class);
        queryWrapper.eq(Parameter::getIsDel, 0);
        if (StringUtils.isNotBlank(parameterQueryRequest.getName())) {
            queryWrapper.like(Parameter::getName, parameterQueryRequest.getName());
        }
        List<Parameter> list = list(queryWrapper);
        PageInfo<Parameter> pageInfo = PageInfo.of(list);

        PageInfo<ParameterResponse> result = PageInfoUtil.convert(pageInfo, parameter -> {
            ParameterResponse parameterResponse = new ParameterResponse();
            BeanUtils.copyProperties(parameter, parameterResponse);
            parameterResponse.setTypeName(ParameterInputTypeEnum.getNameByType(parameterResponse.getType()));
            return parameterResponse;
        });

        return result;
    }

    @Override
    public boolean saveParameter(Parameter parameter) {
        if (parameter.getId() == null) {
            return save(parameter);
        } else {
            LambdaUpdateWrapper<Parameter> updateWrapper = Wrappers.lambdaUpdate(Parameter.class);
            updateWrapper.set(Parameter::getName, parameter.getName())
                    .set(Parameter::getType, parameter.getType())
                    .set(Parameter::getOptions, parameter.getOptions())
                    .set(Parameter::getUpdateTime, LocalDateTime.now())
                    .eq(Parameter::getId, parameter.getId());
            return update(updateWrapper);
        }
    }

    @Override
    public boolean logicDeleteParameter(Integer id) {
        LambdaUpdateWrapper<Parameter> updateWrapper = Wrappers.lambdaUpdate(Parameter.class);
        updateWrapper.set(Parameter::getIsDel, 1)
                .set(Parameter::getUpdateTime, LocalDateTime.now())
                .eq(Parameter::getId, id);
        return update(updateWrapper);
    }
}
